package gen

import (
	"fmt"
	"io/ioutil"
	"log"
	"strings"
)

func GenService(table, module, framework string) {
	switch framework {
	case Ego:
		GenEgoService(table, module, framework)
	default:
		log.Println("generate service undefined:", framework)
	}
}

func GenEgoService(table, module, framework string) {
	var (
		template      = fmt.Sprintf("config/%s/service.template", framework) // 模版文件
		tag_file_name = fmt.Sprintf("%s/%s.go", srvOutPath, table)           // 生成目标文件
		tag_file      = ""                                                   // 文件内容
	)
	bt, err := ioutil.ReadFile(template)
	if err != nil {
		fmt.Println(err)
		return
	}

	tag_file = strings.ReplaceAll(string(bt), "{{module}}", module)

	ioutil.WriteFile(tag_file_name, []byte(tag_file), 0640)

	log.Println(fmt.Sprintf("generate service   file(table <%s> -> {model.%s}): %s", table, module, tag_file_name))
	log.Println("generate service done")
}
